<?php
namespace Mentees\Controller;

class PlanController extends MainController {

    public function index(){


        //学习计划
        $obj = M('Plan');
        $map['menteesID']=$this->userID;

        $count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count,10);
		$show = $page->show();

        $plans=$obj->where($map)->order("startTime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($plans as $key => $value) {
        	unset($map);
        	$map['id']=$value['trainID'];
        	$plans[$key]['trainName']=M("Train")->where($map)->getField("trainItem");
        	$total=M("PlanDetails")->where("planID=".$value['id']."")->count();
        	$comp=M("PlanDetails")->where("planID=".$value['id']." and status=1")->count();
        	$plans[$key]['completed']=round(($comp/$total)*100);
        }
        $this->assign("plans",$plans);
        $this->assign("page",$show);
    	$this->display();
    }

    //查看学习计划详情
    public function view(){
        $id = I('get.id');
        $map['id'] = $id;  
        $plan = M('Plan')->where($map)->find();
        if (!$plan) {
            $this->error(L('_public_noinfo'));
        }else{
            $list = M('PlanDetails')->where('planID='.$id)->order("startTime asc")->select();
            $this->assign('list',$list);  
            $this->assign('plan',$plan); 

            $mentors = M('Mentors')->where('userid='.$plan['mentorsID'])->find();
            $this->assign('mentors',$mentors);

            unset($map);
            $map['id'] = $plan['trainID'];
            $train = M('Train')->where($map)->find();
            $this->assign('train',$train);

            $this->display();
        }
    }
}